package com.ruoyi.course.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.course.domain.CourseStu;
import com.ruoyi.course.service.ICourseParentService;
import com.ruoyi.course.service.ICourseStuService;
import com.ruoyi.course.service.impl.CourseParentServiceImpl;
import com.ruoyi.course.service.impl.CourseStuServiceImpl;
import com.ruoyi.course.vo.CourseStuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 学生管理Controller
 * 
 * @author ruoyi
 * @date 2025-06-12
 */
@RestController
@RequestMapping("/course/courseStu")
public class CourseStuController extends BaseController
{
    @Autowired
    private CourseStuServiceImpl courseStuService;
    @Autowired
    private CourseParentServiceImpl courseParentService;

    /**
     * 查询学生管理列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseStu:list')")
    @GetMapping("/list")
    public TableDataInfo list(CourseStu courseStu)
    {
        startPage();
        List<CourseStu> list = courseStuService.selectCourseStuList(courseStu);
        return getDataTable(list);
    }

    /**
     * 导出学生管理列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseStu:export')")
    @Log(title = "学生管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CourseStu courseStu)
    {
        List<CourseStu> list = courseStuService.selectCourseStuList(courseStu);
        ExcelUtil<CourseStu> util = new ExcelUtil<CourseStu>(CourseStu.class);
        util.exportExcel(response, list, "学生管理数据");
    }

    /**
     * 获取学生管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:courseStu:query')")
    @GetMapping(value = "/{stuId}")
    public AjaxResult getInfo(@PathVariable("stuId") Integer stuId)
    {
        return success(courseStuService.selectCourseStuByStuId(stuId));
    }

    /**
     * 新增学生管理
     */
    @PreAuthorize("@ss.hasPermi('course:courseStu:add')")
    @Log(title = "学生管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CourseStu courseStu)
    {
        return toAjax(courseStuService.insertCourseStu(courseStu));
    }

    /**
     * 修改学生管理
     */
    @PreAuthorize("@ss.hasPermi('course:courseStu:edit')")
    @Log(title = "学生管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CourseStu courseStu)
    {
        return toAjax(courseStuService.updateCourseStu(courseStu));
    }
    //通过id数组批量绑定家长
    @PutMapping("/batchBind")
    public AjaxResult batchBind(@RequestBody CourseStuVo courseStu )
    {

        //批量解绑
         courseStuService.batchupdate(courseStu.getParentId());

        //批量绑定
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("stus",courseStuService.batchBind(courseStu));
        return ajaxResult;
    }

    /**
     * 删除学生管理
     */
    @PreAuthorize("@ss.hasPermi('course:courseStu:remove')")
    @Log(title = "学生管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{stuIds}")
    public AjaxResult remove(@PathVariable Integer[] stuIds)
    {

        return success(courseStuService.deleteCourseStuByStuIds(stuIds));
    }

    @GetMapping("/selectClaIDList")
    public TableDataInfo selectClaIDList(CourseStu courseStu)
    {
        System.out.println(courseStu+"===============================1123==================================================");
        startPage();
        List<CourseStu> list = courseStuService.selectClaIDList(courseStu);
        return getDataTable(list);
    }

}
